package mypackage;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.pane.HorizontalTabController;
import net.rim.device.api.ui.component.pane.HorizontalTabTitleView;
import net.rim.device.api.ui.component.pane.Pane;
import net.rim.device.api.ui.component.pane.PaneManagerController;
import net.rim.device.api.ui.component.pane.PaneManagerModel;
import net.rim.device.api.ui.component.pane.PaneManagerView;
import net.rim.device.api.ui.component.pane.PaneView;
import net.rim.device.api.ui.component.pane.TitleView;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class MyScreen extends MainScreen {
	private XYEdges edgesOne = new XYEdges(1, 1, 1, 1);
	private XYEdges edgesFour = new XYEdges(4, 4, 4, 4);
	private Border borderOne = BorderFactory.createRoundedBorder(edgesOne);
	private Border borderFour = BorderFactory.createRoundedBorder(edgesFour);
	private final int MAX_TITLE_TAB = 6;
	private String leftArrowIcon = "leftArrow.png";
	private String rightArrowIcon = "rightArrow.png";

	/**
	 * Creates a new MyScreen object
	 */
	public MyScreen() {
		// Set the displayed title of the screen
		setTitle("MyTitle");
//		drawShadedFilledPath();
		add(getView());

	}

//	private void drawShadedFilledPath() {
//		Bitmap surface = new Bitmap(240, 160);
//		BitmapField surfaceField = new BitmapField(surface);
//		add(surfaceField);
//		Graphics graphics = new Graphics(surface);
//		int[] X_PTS = { 0, 0, 240, 240 };
//		int[] Y_PTS = { 20, 50, 50, 20 };
//		//int[] drawColors = { 0x0000CC, 0x0000CC, 0xCC0000, 0xCC0000 };
//		int[] drawColors = { 0x0000CC, 0x0000CC, 0x0000CC, 0x0000CC };
//		try {
//			graphics.drawShadedFilledPath(X_PTS, Y_PTS, null, drawColors, null);
//		} catch (IllegalArgumentException iae) {
//			System.out.println("Bad arguments.");
//		}
//	}

	public PaneManagerView getView() {
		PaneManagerModel model = getModel();

		PaneManagerView view = new PaneManagerView(Field.FOCUSABLE,
				getTitleView(model), getPaneView(model));
		view.setController(getController(view, model));
		view.setModel(model);

		model.setView(view);
		model.setController(view.getController());
		return view;
	}

	private PaneManagerModel getModel() {
		PaneManagerModel model = new PaneManagerModel();
		model.enableLooping(true);

		// Create a pane
		for (int i = 0; i < 8; i++) {
			model.addPane(genPane(i));
		}

		model.setCurrentlySelectedIndex(0);
		return model;
	}

	private Pane genPane(int index) {
		VerticalFieldManager vfm = new VerticalFieldManager();
		vfm.add(new LabelField("Label " + index, Field.USE_ALL_WIDTH));
		// vfm.setBorder(borderOne);
		//Pane pane = new Pane(new LabelField("Pane " + index, Field.FOCUSABLE
		//		| Field.FIELD_HCENTER), vfm);
		
		String image = (index%4+1) + ".png";
		Bitmap bitmap = Bitmap.getBitmapResource(image);
		BitmapField bitmapField = new BitmapField(bitmap, Field.FOCUSABLE);
		Pane pane = new Pane(bitmapField, vfm);
		return pane;
	}

	private TitleView getTitleView(PaneManagerModel model) {
		TitleView titleView = new HorizontalTabTitleView(Field.FOCUSABLE);
		((HorizontalTabTitleView) titleView).setNumberOfDisplayedTabs(model
				.numberOfPanes() > MAX_TITLE_TAB ? MAX_TITLE_TAB : model
				.numberOfPanes());
		titleView.setModel(model);
		titleView.setBorder(borderFour);

		Bitmap leftArrow = Bitmap.getBitmapResource(leftArrowIcon);
		Bitmap rightArrow = Bitmap.getBitmapResource(rightArrowIcon);
		if (leftArrow != null) {
			titleView.setLeftArrow(leftArrow);
		}
		if (rightArrow != null) {
			titleView.setRightArrow(rightArrow);
		}

		titleView.setPadding(0, 0, 0, 0);
		titleView.setMargin(0, 0, 0, 0);
		return titleView;
	}

	private PaneView getPaneView(PaneManagerModel model) {
		PaneView paneView = new PaneView(Field.FOCUSABLE);
		paneView.setModel(model);
		return paneView;
	}

	private PaneManagerController getController(PaneManagerView view,
			PaneManagerModel model) {
		PaneManagerController controller = new HorizontalTabController();
		controller.setModel(model);
		controller.setView(view);
		return controller;
	}
}
